
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>PostgreSQL 特有聚合函数 &#8212; Django 3.2.6.dev 文档</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../../../genindex.html" />
    <link rel="search" title="搜索" href="../../../search.html" />
    <link rel="next" title="PostgreSQL 特有数据库约束" href="constraints.html" />
    <link rel="prev" title="django.contrib.postgres" href="index.html" />



 
<script src="../../../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 3.2.6.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="index.html" title="&lt;code class=&#34;docutils literal notranslate&#34;&gt;&lt;span class=&#34;pre&#34;&gt;django.contrib.postgres&lt;/span&gt;&lt;/code&gt;">previous</a>
     |
    <a href="../../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="constraints.html" title="PostgreSQL 特有数据库约束">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-contrib-postgres-aggregates">
            
  <div class="section" id="s-module-django.contrib.postgres.aggregates">
<span id="s-postgresql-specific-aggregation-functions"></span><span id="module-django.contrib.postgres.aggregates"></span><span id="postgresql-specific-aggregation-functions"></span><h1>PostgreSQL 特有聚合函数<a class="headerlink" href="#module-django.contrib.postgres.aggregates" title="永久链接至标题">¶</a></h1>
<p>这些功能可以从 <code class="docutils literal notranslate"><span class="pre">django.contrib.postgres.aggregates</span></code> 模块中获得。在 <a class="reference external" href="https://www.postgresql.org/docs/current/functions-aggregate.html">PostgreSQL docs</a> 中对这些功能有更详细的描述。</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p>所有的函数都没有默认的别名，所以你必须明确提供一个别名。例如：</p>
<div class="last highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">SomeModel</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">aggregate</span><span class="p">(</span><span class="n">arr</span><span class="o">=</span><span class="n">ArrayAgg</span><span class="p">(</span><span class="s1">&#39;somefield&#39;</span><span class="p">))</span>
<span class="go">{&#39;arr&#39;: [0, 1, 2]}</span>
</pre></div>
</div>
</div>
<div class="admonition-common-aggregate-options admonition">
<p class="first admonition-title">通用聚合选项</p>
<p class="last">所有聚合体都有 <a class="reference internal" href="../../models/querysets.html#aggregate-filter"><span class="std std-ref">filter</span></a> 关键字参数。</p>
</div>
<div class="section" id="s-general-purpose-aggregation-functions">
<span id="general-purpose-aggregation-functions"></span><h2>通用聚合函数<a class="headerlink" href="#general-purpose-aggregation-functions" title="永久链接至标题">¶</a></h2>
<div class="section" id="s-arrayagg">
<span id="arrayagg"></span><h3><code class="docutils literal notranslate"><span class="pre">ArrayAgg</span></code><a class="headerlink" href="#arrayagg" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.contrib.postgres.aggregates.ArrayAgg">
<em class="property">class </em><code class="descname">ArrayAgg</code>(<em>expression</em>, <em>distinct=False</em>, <em>filter=None</em>, <em>ordering=()</em>, <em>**extra</em>)<a class="headerlink" href="#django.contrib.postgres.aggregates.ArrayAgg" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个值的列表，包括空值，连接成一个数组。</p>
<dl class="attribute">
<dt id="django.contrib.postgres.aggregates.ArrayAgg.distinct">
<code class="descname">distinct</code><a class="headerlink" href="#django.contrib.postgres.aggregates.ArrayAgg.distinct" title="永久链接至目标">¶</a></dt>
<dd><p>一个可选的布尔参数，用于确定数组值是否会被去重。默认值为 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.postgres.aggregates.ArrayAgg.ordering">
<code class="descname">ordering</code><a class="headerlink" href="#django.contrib.postgres.aggregates.ArrayAgg.ordering" title="永久链接至目标">¶</a></dt>
<dd><p>可选的字段名字符串（可选的 <code class="docutils literal notranslate"><span class="pre">&quot;-&quot;</span></code> 前缀表示降序）或表达式（或字符串和／或表达式的元组或列表），指定结果列表中元素的顺序。</p>
<p>举例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="s1">&#39;some_field&#39;</span>
<span class="s1">&#39;-some_field&#39;</span>
<span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">F</span>
<span class="n">F</span><span class="p">(</span><span class="s1">&#39;some_field&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">desc</span><span class="p">()</span>
</pre></div>
</div>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="s-bitand">
<span id="bitand"></span><h3><code class="docutils literal notranslate"><span class="pre">BitAnd</span></code><a class="headerlink" href="#bitand" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.contrib.postgres.aggregates.BitAnd">
<em class="property">class </em><code class="descname">BitAnd</code>(<em>expression</em>, <em>filter=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.contrib.postgres.aggregates.BitAnd" title="永久链接至目标">¶</a></dt>
<dd><p>返回所有非空输入值的位式 <code class="docutils literal notranslate"><span class="pre">AND</span></code> 的 <code class="docutils literal notranslate"><span class="pre">int</span></code>，如果所有值为空，则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
</dd></dl>

</div>
<div class="section" id="s-bitor">
<span id="bitor"></span><h3><code class="docutils literal notranslate"><span class="pre">BitOr</span></code><a class="headerlink" href="#bitor" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.contrib.postgres.aggregates.BitOr">
<em class="property">class </em><code class="descname">BitOr</code>(<em>expression</em>, <em>filter=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.contrib.postgres.aggregates.BitOr" title="永久链接至目标">¶</a></dt>
<dd><p>返回所有非空输入值的位式 <code class="docutils literal notranslate"><span class="pre">OR</span></code> 的 <code class="docutils literal notranslate"><span class="pre">int</span></code>，如果所有值为空，则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
</dd></dl>

</div>
<div class="section" id="s-booland">
<span id="booland"></span><h3><code class="docutils literal notranslate"><span class="pre">BoolAnd</span></code><a class="headerlink" href="#booland" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.contrib.postgres.aggregates.BoolAnd">
<em class="property">class </em><code class="descname">BoolAnd</code>(<em>expression</em>, <em>filter=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.contrib.postgres.aggregates.BoolAnd" title="永久链接至目标">¶</a></dt>
<dd><p>如果所有输入值为真，返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>，如果所有值为空或没有值，返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>，否则返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Comment</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">body</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TextField</span><span class="p">()</span>
    <span class="n">published</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">BooleanField</span><span class="p">()</span>
    <span class="n">rank</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">IntegerField</span><span class="p">()</span>

<span class="o">&gt;&gt;&gt;</span> <span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">Q</span>
<span class="o">&gt;&gt;&gt;</span> <span class="kn">from</span> <span class="nn">django.contrib.postgres.aggregates</span> <span class="kn">import</span> <span class="n">BoolAnd</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">Comment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">aggregate</span><span class="p">(</span><span class="n">booland</span><span class="o">=</span><span class="n">BoolAnd</span><span class="p">(</span><span class="s1">&#39;published&#39;</span><span class="p">))</span>
<span class="p">{</span><span class="s1">&#39;booland&#39;</span><span class="p">:</span> <span class="kc">False</span><span class="p">}</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">Comment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">aggregate</span><span class="p">(</span><span class="n">booland</span><span class="o">=</span><span class="n">BoolAnd</span><span class="p">(</span><span class="n">Q</span><span class="p">(</span><span class="n">rank__lt</span><span class="o">=</span><span class="mi">100</span><span class="p">)))</span>
<span class="p">{</span><span class="s1">&#39;booland&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">}</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="s-boolor">
<span id="boolor"></span><h3><code class="docutils literal notranslate"><span class="pre">BoolOr</span></code><a class="headerlink" href="#boolor" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.contrib.postgres.aggregates.BoolOr">
<em class="property">class </em><code class="descname">BoolOr</code>(<em>expression</em>, <em>filter=None</em>, <em>**extra</em>)<a class="headerlink" href="#django.contrib.postgres.aggregates.BoolOr" title="永久链接至目标">¶</a></dt>
<dd><p>如果所有输入值为真，返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>，如果所有值为空或没有值，返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>，否则返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
<p>使用实例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Comment</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">body</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TextField</span><span class="p">()</span>
    <span class="n">published</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">BooleanField</span><span class="p">()</span>
    <span class="n">rank</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">IntegerField</span><span class="p">()</span>

<span class="o">&gt;&gt;&gt;</span> <span class="kn">from</span> <span class="nn">django.db.models</span> <span class="kn">import</span> <span class="n">Q</span>
<span class="o">&gt;&gt;&gt;</span> <span class="kn">from</span> <span class="nn">django.contrib.postgres.aggregates</span> <span class="kn">import</span> <span class="n">BoolOr</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">Comment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">aggregate</span><span class="p">(</span><span class="n">boolor</span><span class="o">=</span><span class="n">BoolOr</span><span class="p">(</span><span class="s1">&#39;published&#39;</span><span class="p">))</span>
<span class="p">{</span><span class="s1">&#39;boolor&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">}</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">Comment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">aggregate</span><span class="p">(</span><span class="n">boolor</span><span class="o">=</span><span class="n">BoolOr</span><span class="p">(</span><span class="n">Q</span><span class="p">(</span><span class="n">rank__gt</span><span class="o">=</span><span class="mi">2</span><span class="p">)))</span>
<span class="p">{</span><span class="s1">&#39;boolor&#39;</span><span class="p">:</span> <span class="kc">False</span><span class="p">}</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="s-jsonbagg">
<span id="jsonbagg"></span><h3><code class="docutils literal notranslate"><span class="pre">JSONBAgg</span></code><a class="headerlink" href="#jsonbagg" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.contrib.postgres.aggregates.JSONBAgg">
<em class="property">class </em><code class="descname">JSONBAgg</code>(<em>expressions</em>, <em>distinct=False</em>, <em>filter=None</em>, <em>ordering=()</em>, <em>**extra</em>)<a class="headerlink" href="#django.contrib.postgres.aggregates.JSONBAgg" title="永久链接至目标">¶</a></dt>
<dd><p>将输入值以 <code class="docutils literal notranslate"><span class="pre">JSON</span></code> 数组形式返回。</p>
<dl class="attribute">
<dt id="django.contrib.postgres.aggregates.JSONBAgg.distinct">
<code class="descname">distinct</code><a class="headerlink" href="#django.contrib.postgres.aggregates.JSONBAgg.distinct" title="永久链接至目标">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 3.2.</span> </div>
<p>一个可选的布尔参数，用于确定数组值是否会被去重。默认值为 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.postgres.aggregates.JSONBAgg.ordering">
<code class="descname">ordering</code><a class="headerlink" href="#django.contrib.postgres.aggregates.JSONBAgg.ordering" title="永久链接至目标">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 3.2.</span> </div>
<p>可选的字段名字符串（可选的 <code class="docutils literal notranslate"><span class="pre">&quot;-&quot;</span></code> 前缀表示降序）或表达式（或字符串和／或表达式的元组或列表），指定结果列表中元素的顺序。</p>
<p>例子与 <a class="reference internal" href="#django.contrib.postgres.aggregates.ArrayAgg.ordering" title="django.contrib.postgres.aggregates.ArrayAgg.ordering"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ArrayAgg.ordering</span></code></a> 相同。</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="s-stringagg">
<span id="stringagg"></span><h3><code class="docutils literal notranslate"><span class="pre">StringAgg</span></code><a class="headerlink" href="#stringagg" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.contrib.postgres.aggregates.StringAgg">
<em class="property">class </em><code class="descname">StringAgg</code>(<em>expression</em>, <em>delimiter</em>, <em>distinct=False</em>, <em>filter=None</em>, <em>ordering=()</em>)<a class="headerlink" href="#django.contrib.postgres.aggregates.StringAgg" title="永久链接至目标">¶</a></dt>
<dd><p>返回连接成一个字符串的输入值，用 <code class="docutils literal notranslate"><span class="pre">delimiter</span></code> 字符串分隔。</p>
<dl class="attribute">
<dt id="django.contrib.postgres.aggregates.StringAgg.delimiter">
<code class="descname">delimiter</code><a class="headerlink" href="#django.contrib.postgres.aggregates.StringAgg.delimiter" title="永久链接至目标">¶</a></dt>
<dd><p>必要参数。需要是一个字符串。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.postgres.aggregates.StringAgg.distinct">
<code class="descname">distinct</code><a class="headerlink" href="#django.contrib.postgres.aggregates.StringAgg.distinct" title="永久链接至目标">¶</a></dt>
<dd><p>一个可选的布尔参数，用于确定连接的值是否是不同的。默认值为 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.postgres.aggregates.StringAgg.ordering">
<code class="descname">ordering</code><a class="headerlink" href="#django.contrib.postgres.aggregates.StringAgg.ordering" title="永久链接至目标">¶</a></dt>
<dd><p>可选的字段名字符串（可选的 <code class="docutils literal notranslate"><span class="pre">&quot;-&quot;</span></code> 前缀表示降序）或表达式（或字符串和／或表达式的元组或列表），指定结果字符串中元素的顺序。</p>
<p>例子与 <a class="reference internal" href="#django.contrib.postgres.aggregates.ArrayAgg.ordering" title="django.contrib.postgres.aggregates.ArrayAgg.ordering"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ArrayAgg.ordering</span></code></a> 相同。</p>
</dd></dl>

</dd></dl>

</div>
</div>
<div class="section" id="s-aggregate-functions-for-statistics">
<span id="aggregate-functions-for-statistics"></span><h2>统计的聚合功能<a class="headerlink" href="#aggregate-functions-for-statistics" title="永久链接至标题">¶</a></h2>
<div class="section" id="s-y-and-x">
<span id="y-and-x"></span><h3><code class="docutils literal notranslate"><span class="pre">y</span></code> 和 <code class="docutils literal notranslate"><span class="pre">x</span></code><a class="headerlink" href="#y-and-x" title="永久链接至标题">¶</a></h3>
<p>所有这些函数的参数 <code class="docutils literal notranslate"><span class="pre">y</span></code> 和 <code class="docutils literal notranslate"><span class="pre">x</span></code> 可以是字段名或返回数值数据的表达式。这两个参数都是必须的。</p>
</div>
<div class="section" id="s-corr">
<span id="corr"></span><h3><code class="docutils literal notranslate"><span class="pre">Corr</span></code><a class="headerlink" href="#corr" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.contrib.postgres.aggregates.Corr">
<em class="property">class </em><code class="descname">Corr</code>(<em>y</em>, <em>x</em>, <em>filter=None</em>)<a class="headerlink" href="#django.contrib.postgres.aggregates.Corr" title="永久链接至目标">¶</a></dt>
<dd><p>返回相关系数为 <code class="docutils literal notranslate"><span class="pre">float</span></code>，如果没有任何匹配的记录，则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
</dd></dl>

</div>
<div class="section" id="s-covarpop">
<span id="covarpop"></span><h3><code class="docutils literal notranslate"><span class="pre">CovarPop</span></code><a class="headerlink" href="#covarpop" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.contrib.postgres.aggregates.CovarPop">
<em class="property">class </em><code class="descname">CovarPop</code>(<em>y</em>, <em>x</em>, <em>sample=False</em>, <em>filter=None</em>)<a class="headerlink" href="#django.contrib.postgres.aggregates.CovarPop" title="永久链接至目标">¶</a></dt>
<dd><p>返回总体方差的 <code class="docutils literal notranslate"><span class="pre">float</span></code>，如果没有任何匹配的行，则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
<p>包含一个可选参数：</p>
<dl class="attribute">
<dt id="django.contrib.postgres.aggregates.CovarPop.sample">
<code class="descname">sample</code><a class="headerlink" href="#django.contrib.postgres.aggregates.CovarPop.sample" title="永久链接至目标">¶</a></dt>
<dd><p>默认情况下，<code class="docutils literal notranslate"><span class="pre">CovarPop</span></code> 返回一般总体协方差。但是，如果 <code class="docutils literal notranslate"><span class="pre">sample=True</span></code>，返回值将是样本总体协方差。</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="s-regravgx">
<span id="regravgx"></span><h3><code class="docutils literal notranslate"><span class="pre">RegrAvgX</span></code><a class="headerlink" href="#regravgx" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.contrib.postgres.aggregates.RegrAvgX">
<em class="property">class </em><code class="descname">RegrAvgX</code>(<em>y</em>, <em>x</em>, <em>filter=None</em>)<a class="headerlink" href="#django.contrib.postgres.aggregates.RegrAvgX" title="永久链接至目标">¶</a></dt>
<dd><p>返回自变量的平均值（<code class="docutils literal notranslate"><span class="pre">sum(x)/N</span></code>），作为 <code class="docutils literal notranslate"><span class="pre">float</span></code>，如果没有任何匹配的行，则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
</dd></dl>

</div>
<div class="section" id="s-regravgy">
<span id="regravgy"></span><h3><code class="docutils literal notranslate"><span class="pre">RegrAvgY</span></code><a class="headerlink" href="#regravgy" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.contrib.postgres.aggregates.RegrAvgY">
<em class="property">class </em><code class="descname">RegrAvgY</code>(<em>y</em>, <em>x</em>, <em>filter=None</em>)<a class="headerlink" href="#django.contrib.postgres.aggregates.RegrAvgY" title="永久链接至目标">¶</a></dt>
<dd><p>返回因变量的平均数（<code class="docutils literal notranslate"><span class="pre">sum(y)/N</span></code>），作为 <code class="docutils literal notranslate"><span class="pre">float</span></code>，如果没有任何匹配的行，则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
</dd></dl>

</div>
<div class="section" id="s-regrcount">
<span id="regrcount"></span><h3><code class="docutils literal notranslate"><span class="pre">RegrCount</span></code><a class="headerlink" href="#regrcount" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.contrib.postgres.aggregates.RegrCount">
<em class="property">class </em><code class="descname">RegrCount</code>(<em>y</em>, <em>x</em>, <em>filter=None</em>)<a class="headerlink" href="#django.contrib.postgres.aggregates.RegrCount" title="永久链接至目标">¶</a></dt>
<dd><p>返回两个表达式都不为空的输入行数的 <code class="docutils literal notranslate"><span class="pre">int</span></code>。</p>
</dd></dl>

</div>
<div class="section" id="s-regrintercept">
<span id="regrintercept"></span><h3><code class="docutils literal notranslate"><span class="pre">RegrIntercept</span></code><a class="headerlink" href="#regrintercept" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.contrib.postgres.aggregates.RegrIntercept">
<em class="property">class </em><code class="descname">RegrIntercept</code>(<em>y</em>, <em>x</em>, <em>filter=None</em>)<a class="headerlink" href="#django.contrib.postgres.aggregates.RegrIntercept" title="永久链接至目标">¶</a></dt>
<dd><p>返回由 <code class="docutils literal notranslate"><span class="pre">(x,</span> <span class="pre">y)</span></code> 对确定的最小二乘拟合线性方程的 y 截距，如果没有任何匹配的行，则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
</dd></dl>

</div>
<div class="section" id="s-regrr2">
<span id="regrr2"></span><h3><code class="docutils literal notranslate"><span class="pre">RegrR2</span></code><a class="headerlink" href="#regrr2" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.contrib.postgres.aggregates.RegrR2">
<em class="property">class </em><code class="descname">RegrR2</code>(<em>y</em>, <em>x</em>, <em>filter=None</em>)<a class="headerlink" href="#django.contrib.postgres.aggregates.RegrR2" title="永久链接至目标">¶</a></dt>
<dd><p>以 <code class="docutils literal notranslate"><span class="pre">float</span> <span class="pre">``</span> <span class="pre">的形式返回相关系数的平方，如果没有任何匹配的行，则返回</span> <span class="pre">``None</span></code>。</p>
</dd></dl>

</div>
<div class="section" id="s-regrslope">
<span id="regrslope"></span><h3><code class="docutils literal notranslate"><span class="pre">RegrSlope</span></code><a class="headerlink" href="#regrslope" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.contrib.postgres.aggregates.RegrSlope">
<em class="property">class </em><code class="descname">RegrSlope</code>(<em>y</em>, <em>x</em>, <em>filter=None</em>)<a class="headerlink" href="#django.contrib.postgres.aggregates.RegrSlope" title="永久链接至目标">¶</a></dt>
<dd><p>返回由 <code class="docutils literal notranslate"><span class="pre">(x,</span> <span class="pre">y)</span></code> 对确定的最小二乘拟合线性方程的斜率，如果没有任何匹配的行，则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
</dd></dl>

</div>
<div class="section" id="s-regrsxx">
<span id="regrsxx"></span><h3><code class="docutils literal notranslate"><span class="pre">RegrSXX</span></code><a class="headerlink" href="#regrsxx" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.contrib.postgres.aggregates.RegrSXX">
<em class="property">class </em><code class="descname">RegrSXX</code>(<em>y</em>, <em>x</em>, <em>filter=None</em>)<a class="headerlink" href="#django.contrib.postgres.aggregates.RegrSXX" title="永久链接至目标">¶</a></dt>
<dd><p>返回 <code class="docutils literal notranslate"><span class="pre">sum(x^2)</span> <span class="pre">-</span> <span class="pre">sum(x)^2/N</span></code> （自变量的“平方和”）作为 <code class="docutils literal notranslate"><span class="pre">float</span></code>，如果没有任何匹配的行，则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
</dd></dl>

</div>
<div class="section" id="s-regrsxy">
<span id="regrsxy"></span><h3><code class="docutils literal notranslate"><span class="pre">RegrSXY</span></code><a class="headerlink" href="#regrsxy" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.contrib.postgres.aggregates.RegrSXY">
<em class="property">class </em><code class="descname">RegrSXY</code>(<em>y</em>, <em>x</em>, <em>filter=None</em>)<a class="headerlink" href="#django.contrib.postgres.aggregates.RegrSXY" title="永久链接至目标">¶</a></dt>
<dd><p>返回 <code class="docutils literal notranslate"><span class="pre">sum(x*y)</span> <span class="pre">-</span> <span class="pre">sum(x)</span> <span class="pre">*</span> <span class="pre">sum(y)/N</span></code> （自变量与因变量的乘积之和），如果没有任何匹配的行，则返回 <code class="docutils literal notranslate"><span class="pre">float</span></code>，或者 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
</dd></dl>

</div>
<div class="section" id="s-regrsyy">
<span id="regrsyy"></span><h3><code class="docutils literal notranslate"><span class="pre">RegrSYY</span></code><a class="headerlink" href="#regrsyy" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.contrib.postgres.aggregates.RegrSYY">
<em class="property">class </em><code class="descname">RegrSYY</code>(<em>y</em>, <em>x</em>, <em>filter=None</em>)<a class="headerlink" href="#django.contrib.postgres.aggregates.RegrSYY" title="永久链接至目标">¶</a></dt>
<dd><p>返回 <code class="docutils literal notranslate"><span class="pre">sum(y^2)</span> <span class="pre">-</span> <span class="pre">sum(y)^2/N</span></code> （因变量的“平方和”）作为 <code class="docutils literal notranslate"><span class="pre">float</span></code>，如果没有任何匹配的行，则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
</dd></dl>

</div>
</div>
<div class="section" id="s-usage-examples">
<span id="usage-examples"></span><h2>使用实例：<a class="headerlink" href="#usage-examples" title="永久链接至标题">¶</a></h2>
<p>我们将用这个例表：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">|</span> <span class="n">FIELD1</span> <span class="o">|</span> <span class="n">FIELD2</span> <span class="o">|</span> <span class="n">FIELD3</span> <span class="o">|</span>
<span class="o">|--------|--------|--------|</span>
<span class="o">|</span>    <span class="n">foo</span> <span class="o">|</span>      <span class="mi">1</span> <span class="o">|</span>     <span class="mi">13</span> <span class="o">|</span>
<span class="o">|</span>    <span class="n">bar</span> <span class="o">|</span>      <span class="mi">2</span> <span class="o">|</span> <span class="p">(</span><span class="n">null</span><span class="p">)</span> <span class="o">|</span>
<span class="o">|</span>   <span class="n">test</span> <span class="o">|</span>      <span class="mi">3</span> <span class="o">|</span>     <span class="mi">13</span> <span class="o">|</span>
</pre></div>
</div>
<p>下面是一些通用聚合函数的例子：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">TestModel</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">aggregate</span><span class="p">(</span><span class="n">result</span><span class="o">=</span><span class="n">StringAgg</span><span class="p">(</span><span class="s1">&#39;field1&#39;</span><span class="p">,</span> <span class="n">delimiter</span><span class="o">=</span><span class="s1">&#39;;&#39;</span><span class="p">))</span>
<span class="go">{&#39;result&#39;: &#39;foo;bar;test&#39;}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">TestModel</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">aggregate</span><span class="p">(</span><span class="n">result</span><span class="o">=</span><span class="n">ArrayAgg</span><span class="p">(</span><span class="s1">&#39;field2&#39;</span><span class="p">))</span>
<span class="go">{&#39;result&#39;: [1, 2, 3]}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">TestModel</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">aggregate</span><span class="p">(</span><span class="n">result</span><span class="o">=</span><span class="n">ArrayAgg</span><span class="p">(</span><span class="s1">&#39;field1&#39;</span><span class="p">))</span>
<span class="go">{&#39;result&#39;: [&#39;foo&#39;, &#39;bar&#39;, &#39;test&#39;]}</span>
</pre></div>
</div>
<p>下一个例子显示了统计聚合函数的使用。基本的数学知识将不作说明（例如，你可以在 <a class="reference external" href="https://en.wikipedia.org/wiki/Regression_analysis">wikipedia</a> )：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">TestModel</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">aggregate</span><span class="p">(</span><span class="n">count</span><span class="o">=</span><span class="n">RegrCount</span><span class="p">(</span><span class="n">y</span><span class="o">=</span><span class="s1">&#39;field3&#39;</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="s1">&#39;field2&#39;</span><span class="p">))</span>
<span class="go">{&#39;count&#39;: 2}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">TestModel</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">aggregate</span><span class="p">(</span><span class="n">avgx</span><span class="o">=</span><span class="n">RegrAvgX</span><span class="p">(</span><span class="n">y</span><span class="o">=</span><span class="s1">&#39;field3&#39;</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="s1">&#39;field2&#39;</span><span class="p">),</span>
<span class="gp">... </span>                            <span class="n">avgy</span><span class="o">=</span><span class="n">RegrAvgY</span><span class="p">(</span><span class="n">y</span><span class="o">=</span><span class="s1">&#39;field3&#39;</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="s1">&#39;field2&#39;</span><span class="p">))</span>
<span class="go">{&#39;avgx&#39;: 2, &#39;avgy&#39;: 13}</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">PostgreSQL 特有聚合函数</a><ul>
<li><a class="reference internal" href="#general-purpose-aggregation-functions">通用聚合函数</a><ul>
<li><a class="reference internal" href="#arrayagg"><code class="docutils literal notranslate"><span class="pre">ArrayAgg</span></code></a></li>
<li><a class="reference internal" href="#bitand"><code class="docutils literal notranslate"><span class="pre">BitAnd</span></code></a></li>
<li><a class="reference internal" href="#bitor"><code class="docutils literal notranslate"><span class="pre">BitOr</span></code></a></li>
<li><a class="reference internal" href="#booland"><code class="docutils literal notranslate"><span class="pre">BoolAnd</span></code></a></li>
<li><a class="reference internal" href="#boolor"><code class="docutils literal notranslate"><span class="pre">BoolOr</span></code></a></li>
<li><a class="reference internal" href="#jsonbagg"><code class="docutils literal notranslate"><span class="pre">JSONBAgg</span></code></a></li>
<li><a class="reference internal" href="#stringagg"><code class="docutils literal notranslate"><span class="pre">StringAgg</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#aggregate-functions-for-statistics">统计的聚合功能</a><ul>
<li><a class="reference internal" href="#y-and-x"><code class="docutils literal notranslate"><span class="pre">y</span></code> 和 <code class="docutils literal notranslate"><span class="pre">x</span></code></a></li>
<li><a class="reference internal" href="#corr"><code class="docutils literal notranslate"><span class="pre">Corr</span></code></a></li>
<li><a class="reference internal" href="#covarpop"><code class="docutils literal notranslate"><span class="pre">CovarPop</span></code></a></li>
<li><a class="reference internal" href="#regravgx"><code class="docutils literal notranslate"><span class="pre">RegrAvgX</span></code></a></li>
<li><a class="reference internal" href="#regravgy"><code class="docutils literal notranslate"><span class="pre">RegrAvgY</span></code></a></li>
<li><a class="reference internal" href="#regrcount"><code class="docutils literal notranslate"><span class="pre">RegrCount</span></code></a></li>
<li><a class="reference internal" href="#regrintercept"><code class="docutils literal notranslate"><span class="pre">RegrIntercept</span></code></a></li>
<li><a class="reference internal" href="#regrr2"><code class="docutils literal notranslate"><span class="pre">RegrR2</span></code></a></li>
<li><a class="reference internal" href="#regrslope"><code class="docutils literal notranslate"><span class="pre">RegrSlope</span></code></a></li>
<li><a class="reference internal" href="#regrsxx"><code class="docutils literal notranslate"><span class="pre">RegrSXX</span></code></a></li>
<li><a class="reference internal" href="#regrsxy"><code class="docutils literal notranslate"><span class="pre">RegrSXY</span></code></a></li>
<li><a class="reference internal" href="#regrsyy"><code class="docutils literal notranslate"><span class="pre">RegrSYY</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#usage-examples">使用实例：</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="index.html"
                        title="上一章"><code class="docutils literal notranslate"><span class="pre">django.contrib.postgres</span></code></a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="constraints.html"
                        title="下一章">PostgreSQL 特有数据库约束</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../../../_sources/ref/contrib/postgres/aggregates.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">7月 23, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="index.html" title="&lt;code class=&#34;docutils literal notranslate&#34;&gt;&lt;span class=&#34;pre&#34;&gt;django.contrib.postgres&lt;/span&gt;&lt;/code&gt;">previous</a>
     |
    <a href="../../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="constraints.html" title="PostgreSQL 特有数据库约束">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>